package com.pqtest.api.service;

import com.pqtest.api.domain.AccountDomain;
import com.pqtest.api.dto.AccountDto;
import com.pqtest.api.dto.TestCategoryDto;
import com.pqtest.api.entity.AccountEntity;
import com.pqtest.api.entity.CollectionEntity;
import com.pqtest.api.entity.TestCategoryEntity;
import com.pqtest.api.entity.UserEntity;
import com.pqtest.api.mapper.CollectionMapper;
import com.pqtest.api.mapper.TestCategoryMapper;
import com.pqtest.api.mapper.UserMapper;
import com.pqtest.api.utils.DtoEntityTransformer;
import com.tuyang.beanutils.BeanCopyUtils;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Log
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TestCategoryMapper testCategoryMapper;
    @Autowired
    private CollectionMapper collectionMapper;
    @Autowired
    private AccountDomain accountDomain;

    public long register(AccountDto accountDto) throws Exception {
        accountDto = accountDomain.registerStudent(accountDto);
        AccountEntity accountEntity = new AccountEntity();
        BeanCopyUtils.copyBean(accountDto,accountEntity);
       // DtoEntityTransformer.transalte(accountDto, accountEntity);
        userMapper.createAccount(accountEntity);
        return accountEntity.getId();
    }

    public int changePassword(AccountDto accountDto) throws Exception {
        try {
            AccountEntity accountEntity = userMapper.getAccountByName(accountDto.getName());
            accountDto = accountDomain.updatePassword(accountDto, (AccountDto) BeanCopyUtils.copyBean(accountEntity, new AccountDto()));
            BeanCopyUtils.copyBean(accountDto, accountEntity);
            return userMapper.updatePassword(accountEntity);
        }catch (Exception e ){
            e.printStackTrace();
        }
        return 0;
    }

    public int updateCategory(AccountDto accountDto) throws Exception {
        TestCategoryEntity testCategoryEntity = testCategoryMapper.getTestCategoryById(accountDto.getPqTestCategoryId());
        AccountEntity accountEntity = userMapper.getAccountByName(accountDto.getName());
        BeanCopyUtils.copyBean(accountEntity, accountDto);
        accountDto = accountDomain.updateCategory(accountDto, testCategoryEntity);
        BeanCopyUtils.copyBean(accountDto, accountEntity);
        return userMapper.updateCategory(accountEntity);
    }

    public List<UserEntity> getList(){
        return userMapper.getList();
    }
    @Cacheable(value = "account",key="#p0",unless = "#result == null")
    public AccountEntity getAccountByName(String name){
        return userMapper.getAccountByName(name);
    }

    //@Cacheable(value = "collections",key="#p0",unless = "#result == null")
    public List<CollectionEntity> getColList(int uid){
        return userMapper.getColList(uid);
    }

    //@Cacheable(value = "collections",key="#p0",unless = "#result == null")
    public List<CollectionEntity> getWrongList(int uid){
        return userMapper.getWrongList(uid);
    }

    public UserEntity getById(int uid){
        return userMapper.getById(uid);
    }

}
